home *** CD-ROM | disk | FTP | other *** search
/ Echoes of Ireland / Echoes of Ireland.iso / IRISH_ENGINE / action_script / loadXML_class.as next >
Encoding:
Text File  |  2004-06-10  |  8.4 KB  |  308 lines

  1. /*-----------------------------------------------------------------*\
  2. |    File:            loadXML.as
  3. |    Language:        ActionScript
  4. |    Purpose:        Define generic functions that can be used by
  5. |                    any Flash 5 application to receive
  6. |                    JavaScript Objects from a server via XML.
  7. |
  8. |                    This makes it so that the Flash developer
  9. |                    doesn't have to hand-code all of the logic
  10. |                    for loading and stepping thorugh complex data
  11. |                    structures via XML.  ECMAScript Objects
  12. |                    are much simpler to deal with.
  13. |
  14.  
  15. |
  16. |    Class:            LoadXML
  17. |
  18. |    Constructor:
  19. |        var myXML = new LoadXML();
  20. |                (or)
  21. |        var myXML = new LoadXML(myObject);
  22. |
  23. |    Public Methods:
  24. |        .getFrom( strURL )
  25. |
  26. |    Public Properties:
  27. |        .errorMsg            // string value, containing any error messages after a call
  28. |                            //        to any one of the class methods.  If everything was
  29. |                            //        processed successfully, this property should have a
  30. |                            //        value of "" (empty string).
  31. |        .transferComplete    // boolean (true or false) value, set after a call to the
  32. |                            //     .sentTo() or .getFrom() method.
  33. |        .onLoad                // function reference, pointing to a user-defined function
  34. |                            //         that gets called by LoadXML whenever Flash
  35. |                            //        finishes loading the XML data.
  36. |
  37. |    Private Properties:
  38. |        .object
  39. |        .xmlString
  40. |        .xmlDoc
  41. |
  42. |    Private Functions:
  43. |        XMLDocToObject(oXML)
  44. |        buildObject(obj, eItem)
  45. |
  46. |    Copyright Juxt Interactive, 2001.  All Rights Reserved.
  47. \*-----------------------------------------------------------------*/
  48.  
  49.  
  50. var XML_NODE_TYPE_TEXT = 3;
  51. var XML_NODE_TYPE_ELEMENT = 1;
  52.  
  53. //-------------------------------------------------------------------
  54. // Purpose: given an XML document converts the contents into
  55. //            a JavaScript object.
  56. //-------------------------------------------------------------------
  57. function XMLDocToObject(oXML) {
  58.     trace("XMLDocToObject called");
  59.     var obj = [];
  60.  
  61.     if (oXML == null) {
  62.         return obj;
  63.     }
  64.  
  65.     //--- Step past the root element to the first ARRAY node ---
  66.     var eRoot = oXML.firstChild;
  67.     if (eRoot != null) {
  68.  
  69.         //--- Start with the first node ---
  70.         obj = buildObject(obj, eRoot);
  71.     }
  72.  
  73. //dumpObject(obj, 0, 'XMLDocToObject: obj');
  74.  
  75.     return obj;
  76. }
  77.  
  78. //-------------------------------------------------------------------
  79. // Purpose: Called by XMLDocToObject() function, to recursively build
  80. //            the object from the XML document.
  81. //-------------------------------------------------------------------
  82. function buildObject(obj, eItem) {
  83.  
  84.     var idx, eChild;
  85.     var oTarget;
  86.  
  87.  
  88.     //--- Loop through the sibling elements in this level of the XML ---
  89.     while (eItem != null) {
  90.            idx = eItem.nodeName;
  91.  
  92.         if (eItem.nodeType == XML_NODE_TYPE_ELEMENT) {
  93.  
  94.             //
  95.             //--- Recursively process any other child nodes ---
  96.             oTarget = buildObject( {}, eItem.firstChild);
  97.  
  98.  
  99.             //
  100.             //--- Process any XML node attributes ---
  101.             for (var attrib in eItem.attributes) {
  102.                 oTarget['_attrib_' + attrib] = eItem.attributes[attrib]; // <----------------------------------
  103.             }
  104.  
  105.             //
  106.             //--- Check the first child, and see if it's a simple text node ---
  107.             if (eItem.nodeValue != null ) {
  108.                 //--- Save the value from the TEXT element into the object ---
  109.                 oTarget._value = eItem.nodeValue;
  110.             } else {
  111.                 eChild = eItem.firstChild;
  112.                 if (eChild != null) {
  113.                     if (eChild.nodeType == XML_NODE_TYPE_TEXT) {
  114.                         if (eChild.nodeValue != null) {
  115.                             //--- Save the value from the TEXT element into the object ---
  116.                             oTarget._value = eChild.nodeValue;
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             //
  123.             //--- If there are duplicate nodenames, convert them to an array ---
  124.             //if (obj[idx] != null || eChild.parentNode.nodeName == "EPSSPOA") {
  125.             if (obj[idx] != null ) {
  126.  
  127.                 if (obj[idx]._type != 'array') {
  128.                 //    oTarget = new Array();
  129.                 //    oTarget[0] = obj[idx];
  130.                 //    obj[idx] = oTarget;
  131.  
  132.                     obj[idx] = [ obj[idx] ];
  133.                     obj[idx]._type = 'array';
  134.                     obj[idx][1] = oTarget;
  135.                 } else {
  136.                     obj[idx][ obj[idx].length ] = oTarget;
  137.                 }
  138.             } else {
  139.                 obj[idx] = oTarget;
  140.             }
  141.  
  142.         }    //*** END OF: if (eItem.nodeType == XML_NODE_TYPE_ELEMENT) ***
  143.  
  144.  
  145.  
  146.         //-- Get the next sibling node in this level of the XML ---
  147.         eItem = eItem.nextSibling;
  148.  
  149.     }    //*** END OF: while(eItem != null) ***
  150.  
  151.     return obj;
  152. }
  153.  
  154. //-------------------------------------------------------------------
  155. // Purpose: Class constructor for LoadXML Class.
  156. //-------------------------------------------------------------------
  157. function LoadXML (strURL) {
  158.  
  159.  
  160.     //this.onLoad = fpHandlerFunction;
  161.  
  162.     this.xmlString = "";
  163.     this.errorMsg = "";
  164.     this.xmlDoc = new XML();
  165.  
  166.     this.getFrom(strURL)
  167.  
  168.     this.onLoad = function (){
  169.     trace("on data loaded called");
  170.     if ("" == this.errorMsg) {
  171.         //oXMLLoader.xmlObject = new Object();
  172.         this.xmlObject = this.object;
  173.         //
  174.         //
  175.         // --- NOTE: if you want to see a trace of the object after
  176.         // --- it has been constructed from the XML, then
  177.         // --- unremark the following line of code...
  178.         //
  179.         // dumpObject(oXMLLoader.object, 0, 'oXMLLoader.object');
  180.         //
  181.     } else {
  182.         trace ('error loading data: '+this.errorMsg);
  183.     }
  184.     _level0.dataLoaded = true;
  185.     }
  186.  
  187.     return this;
  188. }
  189.  
  190. //-------------------------------------------------------------------
  191. // Purpose: Starts the process of loading an object (via XML) from
  192. //            a URL.
  193. // Notes: The object does not become available immediately upon return
  194. //            of this function.  The transfer starts running in the
  195. //            background.  When the object has been loaded, the
  196. //            .transferComplete property of the LoadXML will be
  197. //            true.
  198. //            example:
  199. //                if(myAryTrans.transferComplete == true) {
  200. //                    gotoAndPlay('continue');
  201. //                }
  202. //-------------------------------------------------------------------
  203. LoadXML.prototype.getFrom = function( strURL ) {
  204.  
  205.     //--- Make sure the caller passed a URL ---
  206.     if (null == strURL) {
  207.         this.errorMsg = "object.getFrom() requires a 'strURL' parameter.";
  208.         return;
  209.     }
  210.  
  211.     if (null == this.xmlDoc) {
  212.         this.xmlDoc = new XML();
  213.     }
  214.  
  215.  
  216.     this.errorMsg = "";
  217.  
  218.     //--- Set up an event handler for the response document ---
  219.     this.xmlDoc.onLoad = onXMLLoaded;
  220.     this.xmlDoc.refToLoadXMLObj = this;
  221.  
  222.     //--- Initialize the .transferComplete property so the loader can check when we're done ---
  223.     this.transferComplete = false;
  224.  
  225.     //--- Start the asynchronous data transfer with the server ---
  226.     this.xmlDoc.load(strURL);
  227.  
  228.     return;
  229. }
  230.  
  231. //--- Attach this function definition to the Class, as a method ---
  232. //LoadXML.prototype.getFrom = _getFrom;
  233.  
  234.  
  235. //-------------------------------------------------------------------
  236. // Purpose:  handle onLoad event from XML.load() function.
  237. //-------------------------------------------------------------------
  238. function onXMLLoaded() {
  239.     trace("onXMLLoaded called");
  240.     var oXML = this;
  241.  
  242.     //--- extract a pointer to the original LoadXML object from the XML object ---
  243.     var oLoadXML = this.refToLoadXMLObj;
  244.  
  245.     //--- Convert the XML document to an object ----
  246.     oLoadXML.object = XMLDocToObject( oXML );
  247.  
  248.     //--- Set a flag to let outsiders know we're done! ---
  249.     oLoadXML.transferComplete = true;
  250.  
  251.     //--- If a handler function has been assigned, call it now ---
  252.     if (typeof(oLoadXML.onLoad) == 'function') {
  253.         oLoadXML.onLoad( oLoadXML );
  254.     }
  255.  
  256. }
  257.  
  258. //----------------------------------------------------------------------------
  259. // --- debugging function....
  260. function dumpObject( obj, nLevels, sName ) {
  261.     var idx, sPre = '';
  262.     if (nLevels == null) {
  263.         nLevels = 0;
  264.     }
  265.     for (idx=1; idx<=nLevels; idx++) {
  266.         sPre += '\t';
  267.     }
  268.     trace(sPre + sName + '{');
  269.     nLevels++;
  270.     for (idx in obj) {
  271.         if (typeof(obj[idx]) == 'object') {
  272.             dumpObject( obj[idx], nLevels, idx );
  273.         } else {
  274.             trace(sPre + idx + '="' + obj[idx] + '"');
  275.         }
  276.     }
  277.  
  278.     trace(sPre + '}');
  279.  
  280. }
  281.  
  282.  
  283. //
  284. // ----------------------------------------------------------------------------
  285. // Function:  onDataLoaded()
  286. // Purpose:   Gets called by the LoadXML object, when the XML data has
  287. // finished loading
  288. // ----------------------------------------------------------------------------
  289. function onDataLoaded (oXMLLoader) {
  290.     if ("" == oXMLLoader.errorMsg) {
  291.         xmlObject = oXMLLoader.object;
  292.         //
  293.         //
  294.         // --- NOTE: if you want to see a trace of the object after
  295.         // --- it has been constructed from the XML, then
  296.         // --- unremark the following line of code...
  297.         //
  298.          //dumpObject(oXMLLoader.object, 0, 'oXMLLoader.object');
  299.         //
  300.     } else {
  301.         trace ('error loading data: '+oXMLLoader.errorMsg);
  302.     }
  303.     dataLoaded = true;
  304. }
  305.  
  306.  
  307.  
  308.